"""
Case Type   : 多块预读
Case Name   : 开启多块预读，vacuum
Create At   : 2024/12/04
Owner       : @li-xin12345
Description :
    1、建表，重启
    2、设置预读，vacuum
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SessionTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.tb_name = 't_multi_bulk_read_0014'

    def test_multi_bulk_read(self):
        text = '----step1: 建表，重启；expect: 成功----'
        self.log.info(text)
        sql = f'''drop table if exists t_multi_bulk_read_0014 cascade;
        create table t_multi_bulk_read_0014 (
            c_int int,
            c_char char(10),
            c_bool bool default false
        );
        insert into t_multi_bulk_read_0014
        values (generate_series(1, 1000), 'char' || generate_series(1, 1000));
        '''
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text}')
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      res, f'执行失败：{text}')
        self.assertIn('INSERT 0 1000', res, f'执行失败：{text}')

        msg = self.pri_sh.restart_db_cluster()
        self.log.info(msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        text = '----step2: 设置预读，vacuum；expect: 成功----'
        self.log.info(text)
        sql = f"alter system set vacuum_bulk_read_size = 16;" \
              f"vacuum;"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.alter_system_success_msg,
                      res, f'执行失败：{text}')
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, res, f'执行失败：{text}')
        msg = self.pri_sh.restart_db_cluster()
        self.log.info(msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        sql = f"alter system set vacuum_bulk_read_size = 8;" \
              f"truncate table {self.tb_name};" \
              f"vacuum analyze {self.tb_name};"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.alter_system_success_msg,
                      res, f'执行失败：{text}')
        self.assertIn(self.constant.TRUNCATE_SUCCESS_MSG,
                      res, f'执行失败：{text}')
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, res, f'执行失败：{text}')

    def tearDown(self):
        text = '----step3: 清理环境;expect:成功----'
        self.log.info(text)
        sql = f"drop table if exists {self.tb_name} cascade;" \
              f"alter system set vacuum_bulk_read_size = 0;"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text}')
        self.assertIn(self.constant.alter_system_success_msg,
                      res, f'执行失败：{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')